<!DOCTYPE html>
<html lang="ch-CN">
<head>
  <meta charset="UTF-8">
  <title>查询构造函数中实例化对象年龄大于18的人</title>
</head>
<style>
  body {
    width: 100%;
    height: 100vh;
    background-color: wheat;
    font-size: 30px;
    text-decoration: none;
    font-family: "华文楷体";
  }
</style>
<script>

  function Person(name, age, gender) {
    this.name = name
    this.age = age
    this.gender = gender
  }

  // 重构构造函数的原型对象的toString方法 否则输出的是{object Object}
  Person.prototype.toString = function () {
      return "name:" + this.name + "age:" + this.age + "gender:" + this.gender + "<br>"
      // ("name:" + this.name + "age:" + this.age + "gender:" + this.gender + "<br>")
  }

  // Person的分类对象实例化
  let Person1 = new Person("小红", 10, "女")
  let Person2 = new Person("小兰", 30, "女")
  let Person3 = new Person("小紫", 26, "男")
  let Person4 = new Person("小花", 16, "男")

  // 将对象装入数组Arr中
  let Arr = [Person1, Person2, Person3, Person4]

  // 构造一个查询年龄大于18的构造函数
  function Found(arr) {
    let newArr = []
    for (let i = 0; i < arr.length; i++) {
      // 查询年龄大于18的构造函数
      if (arr[i].age >= 18) {
        // 向新的数组添加年龄大于18的对象
        newArr.push(arr[i])
      }
    }
    // 将新数组返回
    return newArr
  }
  // 实例化一个result对象
  let result=new Found(Arr)
  /*
      * 将对象输出  会隐性调用toString方法,默认的toString方法输出的是{object Object}
      * 所以上面我们将toString方法重构
  */
  console.log(result)
  document.write(result.toString())
</script>
<body>
</body>
</html>